<template>
  <div class="app-container">
    <el-card>
      <el-tabs v-model="activeTab">
        <!-- 个人信息 -->
        <el-tab-pane label="个人信息" name="basic">
          <div class="user-info-container">
            <div class="user-info-header">
              <el-avatar :size="120" :src="userInfo.avatar" class="user-avatar"></el-avatar>
              <h3>{{ userInfo.nickName || userInfo.userName }}</h3>
            </div>
            
            <div class="user-info-details">
              <el-row :gutter="20">
                <el-col :span="12">
                  <div class="info-card">
                    <h4>基本信息</h4>
                    <div class="info-item">
                      <span class="info-label">用户ID:</span>
                      <span class="info-value">{{ userInfo.userId }}</span>
                    </div>
                    <div class="info-item">
                      <span class="info-label">用户名:</span>
                      <span class="info-value">{{ userInfo.userName }}</span>
                    </div>
                    <div class="info-item">
                      <span class="info-label">用户昵称:</span>
                      <span class="info-value">{{ userInfo.nickName }}</span>
                    </div>
                    <div class="info-item">
                      <span class="info-label">性别:</span>
                      <span class="info-value">{{ userInfo.sex === '0' ? '男' : '女' }}</span>
                    </div>
                  </div>
                </el-col>
                <el-col :span="12">
                  <div class="info-card">
                    <h4>联系信息</h4>
                    <div class="info-item">
                      <span class="info-label">手机号码:</span>
                      <span class="info-value">{{ userInfo.phonenumber }}</span>
                    </div>
                    <div class="info-item">
                      <span class="info-label">邮箱:</span>
                      <span class="info-value">{{ userInfo.email }}</span>
                    </div>
                  </div>
                </el-col>
              </el-row>
              <div class="info-card">
                <h4>账户信息</h4>
                <div class="info-item">
                  <span class="info-label">状态:</span>
                  <span class="info-value">{{ userInfo.status }}</span>
                </div>
                <div class="info-item">
                  <span class="info-label">创建时间:</span>
                  <span class="info-value">{{ userInfo.createTime }}</span>
                </div>
              </div>
            </div>
            
            <div class="user-info-actions">
              <el-button type="primary" class="blue-border-button" @click="showEditDialog">修改信息</el-button>
            </div>
          </div>
        </el-tab-pane>

        <!-- 编辑用户信息对话框 -->
        <el-dialog v-model="editDialogVisible" title="修改个人信息" width="500px">
          <el-form ref="editForm" :model="editForm" label-width="100px">
            <el-form-item label="头像">
              <el-upload
                class="avatar-uploader"
                action=""
                :show-file-list="false"
                :auto-upload="false"
                :on-change="handleAvatarChange">
                <el-avatar :size="120" :src="editForm.avatar" class="user-avatar"></el-avatar>
              </el-upload>
            </el-form-item>
            <el-form-item label="用户昵称" prop="nickName">
              <el-input v-model="editForm.nickName" />
            </el-form-item>
            <el-form-item label="手机号码" prop="phonenumber">
              <el-input v-model="editForm.phonenumber" />
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="editForm.email" />
            </el-form-item>
            <el-form-item label="性别" prop="sex">
              <el-radio-group v-model="editForm.sex">
                <el-radio label="0">男</el-radio>
                <el-radio label="1">女</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-form>
          <template #footer>
            <el-button @click="editDialogVisible = false">取消</el-button>
            <el-button type="primary" @click="updateUserInfo">保存</el-button>
          </template>
        </el-dialog>

        <!-- 修改密码 -->
        <el-tab-pane label="修改密码" name="password">
          <el-form ref="passwordForm" :model="passwordForm" label-width="100px">
            <el-form-item label="旧密码">
              <el-input v-model="passwordForm.oldPassword" type="password" />
            </el-form-item>
            <el-form-item label="新密码">
              <el-input v-model="passwordForm.newPassword" type="password" />
            </el-form-item>
            <el-form-item label="确认密码">
              <el-input v-model="passwordForm.confirmPassword" type="password" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" class="blue-border-button" @click="updatePassword">提交</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import useUserStore from '../../store/modules/user'
import dayjs from 'dayjs'

const userStore = useUserStore()
const activeTab = ref('basic')

const userInfo = ref({
  userId: 0,
  userName: '',
  avatar: '',
  nickName: '',
  phonenumber: '',
  email: '',
  sex: '0',
  status: '',
  delFlag: '',
  createTime: ''
})

const passwordForm = ref({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

onMounted(async () => {
  await userStore.userInfo()
  userInfo.value = {
    userId: userStore.userId || 0,
    userName: userStore.username,
    avatar: userStore.avatar,
    nickName: userStore.nickName || '',
    phonenumber: userStore.phonenumber || '',
    email: userStore.email || '',
    sex: userStore.sex || '0',
    status: userStore.status || '',
    delFlag: userStore.delFlag || '',
    createTime: userStore.createTime ? dayjs(userStore.createTime).format('YYYY-MM-DD HH:mm:ss') : '2025-03-24 00:00:00'
  }
})

const updateUserInfo = async () => {
  try {
    // Update user info with edited values
    userInfo.value = {
      ...userInfo.value,
      nickName: editForm.value.nickName,
      phonenumber: editForm.value.phonenumber,
      email: editForm.value.email,
      sex: editForm.value.sex,
      ...(editForm.value.avatar && { avatar: editForm.value.avatar })
    }
    
    // TODO: 调用API更新用户信息
    ElMessage.success('用户信息更新成功')
    editDialogVisible.value = false
  } catch (error) {
    ElMessage.error('用户信息更新失败')
  }
}

const editDialogVisible = ref(false)
const editForm = ref({
  avatar: '',
  nickName: '',
  phonenumber: '',
  email: '',
  sex: '0'
})

const showEditDialog = () => {
  editForm.value = {
    avatar: userInfo.value.avatar,
    nickName: userInfo.value.nickName,
    phonenumber: userInfo.value.phonenumber,
    email: userInfo.value.email,
    sex: userInfo.value.sex
  }
  editDialogVisible.value = true
}

const handleAvatarChange = (file: { raw: File }) => {
  const reader = new FileReader()
  reader.onload = () => {
    if (reader.result && typeof reader.result === 'string') {
      editForm.value.avatar = reader.result
    }
  }
  reader.readAsDataURL(file.raw)
}

const updatePassword = async () => {
  if (passwordForm.value.newPassword !== passwordForm.value.confirmPassword) {
    ElMessage.error('两次输入的密码不一致')
    return
  }
  try {
    // TODO: 调用API修改密码
    ElMessage.success('密码修改成功')
    passwordForm.value = {
      oldPassword: '',
      newPassword: '',
      confirmPassword: ''
    }
  } catch (error) {
    ElMessage.error('密码修改失败')
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
  height: 100%;
  
  /* Custom scrollbar */
  ::-webkit-scrollbar {
    width: 8px;
    height: 8px;
  }
  ::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 4px;
  }
  ::-webkit-scrollbar-thumb {
    background: #c1c1c1;
    border-radius: 4px;
  }
  ::-webkit-scrollbar-thumb:hover {
    background: #a8a8a8;
  }

  .el-tabs {
    :deep(.el-tabs__header) {
      margin: 0;
    }
    
    :deep(.el-tabs__content) {
      padding: 20px;
      max-height: calc(100vh - 200px);
      overflow-y: auto;
    }
  }
  
  .el-form {
    max-width: 600px;
    margin: 20px auto;
    
    .el-form-item {
      margin-bottom: 22px;
    }
    
    .el-input {
      max-width: 400px;
    }

    .user-avatar {
      margin: 20px auto;
      border: 4px solid #fff;
      box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
    }
  }
}

.blue-border-button {
  background-color: #409EFF;
  color: #fff;
  border-radius: 4px;
  padding: 10px 20px;
  border: none;
  transition: all 0.3s ease;
}

.blue-border-button:hover {
  background-color: #66b1ff;
}

.blue-border-button:active {
  background-color: #3a8ee6;
}

@media (max-width: 768px) {
  .app-container {
    padding: 10px;
  }
}

.user-info-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  
  .user-info-header {
    text-align: center;
    margin-bottom: 30px;
    
    .user-avatar {
      margin-bottom: 15px;
      border: 4px solid #fff;
      box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
    }
  }
  
  .user-info-details {
    margin-bottom: 30px;
    
    .info-card {
      background: #fff;
      border-radius: 8px;
      padding: 20px;
      margin-bottom: 20px;
      box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
      
      h4 {
        margin: 0 0 15px 0;
        color: #409EFF;
        font-size: 16px;
        padding-bottom: 10px;
        border-bottom: 1px solid #eee;
      }
    }
    
    .info-item {
      display: flex;
      margin-bottom: 12px;
      
      .info-label {
        width: 80px;
        color: #909399;
        font-weight: 500;
      }
      
      .info-value {
        flex: 1;
        color: #606266;
      }
    }
  }
  
  .user-info-actions {
    text-align: center;
    margin-top: 30px;
  }
}
</style>
